(function ($) {
    $.fn.comboMenu = function (content) {
        var me = $(this);
        me.click(function () {
            var offset = me.offset();
            content.css({
                left: offset.left + "px",
                top: offset.top + me.outerHeight() + "px"
            }).slideDown("fast");
            $("body").bind("mousedown", onMousedown);
            return false;
        });
        function onMousedown(event) {
            var parents = $(event.target).parents(content);
            var over = false;
            for (var i = 0; i < parents.length; i++) {
                var dom = parents[i];
                if (dom == content[0]) {
                    over = true;
                }
                console.log(dom == content[0]);
            }
            if (!over) {
                content.fadeOut("fast");
                $("body").unbind("mousedown", onMousedown);
            }
        }
    }
})(jQuery);


var setting = {
    async: {
        enable: true,
        url: getUrl,
        type: 'get'
    },
//            check: {
//                enable: true
//            },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        expandSpeed: ""
    },
    callback: {
        onClick: onClick,
        onAsyncError: onAsyncError
    }
};

var zNodes = [
    {name: "团中央", id: "1", count: 500, times: 1, isParent: true}
];

var log, className = "dark",
    startTime = 0, endTime = 0, perCount = 100, perTime = 100;

function onClick(e, treeId, treeNode) {
    //var zTree = $.fn.zTree.getZTreeObj("#treeDemo"),
    //    nodes = zTree.getSelectedNodes(),
    //    v = "";
    //nodes.sort(function compare(a, b) {
    //    return a.id - b.id;
    //});
    //for (var i = 0, l = nodes.length; i < l; i++) {
    //    if (nodes[i].selectable) {
    //        v += nodes[i].name + ",";
    //    }
    //}
    //if (v.length > 0) v = v.substring(0, v.length - 1);
    //var cityObj = $("#citySel");
    //cityObj.attr("value", v);
    //if (v.length > 0) {
    //    hideMenu();
    //}
    hideMenu();
}

function getUrl(treeId, treeNode) {
    var curCount = (treeNode.children) ? treeNode.children.length : 0;
    var getCount = (curCount + perCount) > treeNode.count ? (treeNode.count - curCount) : perCount;
    var param = "id=" + treeNode.id + "_" + (treeNode.times++) + "&count=" + getCount;
    return "http://localhost:8888/platform/orgs/2";
}


function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    alert("异步获取数据出现异常。");
    treeNode.icon = "";
    zTree.updateNode(treeNode);
}


function showMenu() {
    var cityObj = $("#citySel");
    var cityOffset = $("#citySel").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

function hideMenu() {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
        hideMenu();
    }
}

function showDialog() {
    var d = dialog({
        title: '整体转移',
        ok: function () {
            alert(1);
        },
        cancel: true,
        okValue: '确定',
        cancelValue: '取消',
        content: '<div>原组织：<input readonly value="佛山市第四中学团支部" style="width:150px;"/></div>' +
        '<div style="margin-top:10px">转移至：<input id="citySel" type="text" readonly value="" style="width:150px;" onclick="showMenu(); return false;"/></div>'
    });
    d.show();
}


$(function () {
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    $('#test').comboMenu($("#menuContent"));
});